package com.changgou.web.controller;


import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller

@RequestMapping("/wcart")


public class CartController {



    @Autowired
    private CartFeign cartFeign;

    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public String list(Model model){

        //封装了三个参数:
        //1.当前列表的数据
        //2.当前的购物车的总数量
        //3.当前购物车的总价
        Map map = cartFeign.list();

        model.addAttribute("items",map);

        return "cart";


    }


    /**
     *  添加购物车
     * @param id    商品id
     * @param num   商品数量
     * @return
     */

    @GetMapping("/add")
    @ResponseBody
    public Result<Map> add(String id, Integer num){
        cartFeign.addCart(id,num);

        //跟新购物车数据
        Map map = cartFeign.list();

        return new Result<>(true, StatusCode.OK,"添加购物车成功",map);


    }

}
